<template>
  <el-card class="box-card">
    <!-- 头部 -->
    <div slot="header" class="clearfix">
      <el-button @click="$router.push({ name: 'menu-creat' })"
        >添加菜单</el-button
      >
    </div>
    <!-- 表格 -->
    <el-table :data="menuData" border style="width: 100%">
      <el-table-column type="index" label="编号" width="180" align="center">
      </el-table-column>
      <el-table-column prop="name" label="菜单名称" width="180" align="center">
      </el-table-column>
      <el-table-column prop="level" label="菜单级数" align="center">
      </el-table-column>
      <el-table-column prop="icon" label="菜单图标" align="center">
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" align="center">
      </el-table-column>
      <el-table-column prop="" label="操作" align="center">
        <!-- 编辑、删除按钮 -->
        <template v-slot="scoped">
          <el-button
            size="mini"
            @click="
              $router.push({ name: 'menu-edit', params: { id: scoped.row.id } })
            "
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scoped.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script>
//导入获取菜单的函数
import { getAllMenus, deleteMenu } from "@/api/menu";
export default {
  name: "MenuIndex",
  data() {
    return {
      menuData: [],
    };
  },
  created() {
    this.loadMenus();
  },
  methods: {
    loadMenus() {
      getAllMenus().then((res) => {
        if (res.data.code === "000000") {
          //保存所有数据
          this.menuData = res.data.data;
        } else {
          this.$message.error("请求所有菜单信息失败");
        }
        // console.log("22", res);
      });
    },
    handleDelete(menuId) {
      //console.log("获得删除行的id", menuId);
      this.$confirm("确认要删除菜单吗？", "删除提示")
        .then(() => {
          //如果点确认,执行这里
          deleteMenu(menuId).then((res) => {
            if (res.data.data) {
              this.$message.success("删除菜单成功!");
              //向服务器要最新的信息
              this.loadMenus();
            } else {
              this.$message.error("删除菜单失败~");
            }
            console.log("删除菜单", res);
          });
        })
        .catch(() => {
          //如果点取消,执行这里
          this.$message.info("取消了删除菜单的操作...");
        });
    },
  },
};
</script>
<style lang="scss" scoped></style>
